package test;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.request.AlibabaAliqinFcTtsNumSinglecallRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.taobao.api.response.AlibabaAliqinFcTtsNumSinglecallResponse;

public class TestTaobao {

	public static void main(String[] args) {
		String appkey = "23551047";
		String secret = "a05716d00b6753f900779c16e71f1da7";
		String tel = "15001236918";
		String extend = "12345";
		String code = "5678";
		String product = "SMS测试服务";
		
		
		TestTaobao test=new TestTaobao();
		boolean b1 = test.sendCodeVoice(appkey, secret, code, product, tel, extend, "01053912808", "TTS_32435026");
		boolean b2 = test.sendCodeSms(appkey, secret, code, product, tel, "深思云", extend, "SMS_34970119");
		boolean b3 = test.sendSms(appkey, secret, new String[]{"h16","2016-12-20 10:10:10","开发者网站许可操作相关测试"}, tel, "深思云", extend, "SMS_34945052");
		System.out.println(b1+"  "+b2+"  "+b3);
	}

	private String url = "http://gw.api.taobao.com/router/rest";

	public boolean sendCodeSms(String appkey, String secret, String code, String product, String tel, String signs,
			String extend, String template) {
		boolean b = false;
		try {
			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret, "json", 15000, 30000, "md5");
			AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
			req.setExtend(extend);
			req.setSmsType("normal");
			req.setSmsFreeSignName(signs);
			req.setSmsParamString("{code:'" + code + "',product:'" + product + "'}");
			req.setRecNum(tel);
			req.setSmsTemplateCode(template);
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);

			String respStr = rsp.getBody();

			if (respStr.contains("\"success\":true")) {
				b = true;
			} else {
			}
		} catch (Exception e) {
		}
		return b;
	}

	public boolean sendCodeVoice(String appkey, String secret, String code, String product, String tel, String extend,
			String showNum, String template) {
		boolean b = false;
		try {
			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
			AlibabaAliqinFcTtsNumSinglecallRequest req = new AlibabaAliqinFcTtsNumSinglecallRequest();
			req.setExtend(extend);
			req.setTtsParamString("{product:'" + product + "',code:'" + code + "'}");
			req.setCalledNum(tel);
			req.setCalledShowNum(showNum);
			req.setTtsCode(template);
			AlibabaAliqinFcTtsNumSinglecallResponse rsp = client.execute(req);

			String respStr = rsp.getBody();

			if (respStr.contains("\"success\":true")) {
				b = true;
			} else {
			}

		} catch (Exception e) {
		}
		return b;
	}

	public boolean sendSms(String appkey, String secret, String[] txtary, String tel, String signs, String extend,
			String template) {
		boolean b = false;
		try {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < txtary.length; i++) {
				sb.append("p" + (i + 1) + ":'" + txtary[i] + "',");
			}
			String code = sb.toString();
			if (code.length() > 1) {
				code = code.substring(0, code.length() - 1);
			}

			TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret, "json", 15000, 30000, "md5");
			AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
			req.setExtend(extend);
			req.setSmsType("normal");
			req.setSmsFreeSignName(signs);
			req.setSmsParamString("{" + code + "}");
			req.setRecNum(tel);
			req.setSmsTemplateCode(template);
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);

			String respStr = rsp.getBody();

			if (respStr.contains("\"success\":true")) {
				b = true;
			} else {
			}
		} catch (Exception e) {
		}
		return b;
	}

}
